package com.itqf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itqf.entity.TbItem;
import com.itqf.mapper.TbItemMapper;
import com.itqf.service.TbItemService;
import com.itqf.utils.R;
import com.itqf.vo.QueryParam;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description:
 * @Company: 千锋互联
 * @Author: 李丽婷
 * @Date: 2020/11/17
 * @Time: 下午4:54
 */
@Service //默认：tbItemServiceImpl
public class TbItemServiceImpl implements TbItemService {
    
    //依赖mapper
    @Resource
    private TbItemMapper tbItemMapper;
    
    @Override
    public List<TbItem> findAll() {
        return tbItemMapper.findAll();
    }

    @Override
    public R add(TbItem item) {
        return tbItemMapper.insert(item)>0?R.ok("新增成功"):R.error("新增失败");
    }

    @Override
    public R findByPage(QueryParam param) {
       // PageHelper.startPage(int pageNum, int pageSize);  pageNum：查看的页码
        //limit :每页显示多少条  offset：起始记录
        //int offset, int limit
        PageHelper.offsetPage(param.getOffset(),param.getLimit())  ;
        //分页+条件查询+排序
        List<TbItem> list = tbItemMapper.findByPage(param);

        //分页后的信息
        PageInfo pageInfo = new PageInfo(list);
        list = pageInfo.getList();//当前页的集合
        long total = pageInfo.getTotal();//总的记录数

        return R.ok().put("total",total).put("rows",list);
    }

    @Override
    public R update(TbItem item) {
        return tbItemMapper.update(item)>0?R.ok():R.error();
    }

    @Override
    public R deleteBatch(List<Integer> ids) {
        return tbItemMapper.deleteBatch(ids)>0?R.ok("删除成功"):R.error("删除失败");
    }
}
